package com.controller.ht;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.UuidUtil;
import com.controller.base.BaseController;
import com.entity.AppendixRelationship;
import com.entity.NewsColumn;
import com.service.AppendixRelationshipService;
import com.service.newsColumnService;

@Controller
@RequestMapping("/newsColumn/")
public class newsColumnController extends BaseController<Object>{

	@Autowired
	private newsColumnService ncservice;
	@Autowired
	private AppendixRelationshipService apprpservice;
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/findAllUI")
	@ResponseBody
	public AjaxRes findAllUI(NewsColumn nc){
		System.out.println("================查询所有新闻栏目");
		AjaxRes res = new AjaxRes();
		List<NewsColumn> ncs = ncservice.find(nc);
		res.setSucceed(ncs,"数据获取成功");
		return res;
	}
	/**
	 * 分页查询
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(NewsColumn nc,Page<NewsColumn> p){
		AjaxRes res = new AjaxRes();
		Page<NewsColumn> pageUis = ncservice.findByPage(nc, p);
		// 获得SimpleDateFormat类
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < pageUis.getResults().size(); i++) {
			pageUis.getResults().get(i).setCreateTimeToString(sf.format(pageUis.getResults().get(i).getCreateTime()));
		}
		res.setSucceed(pageUis, "数据获取成功");
		return res;
	}
	/**
	 * 修改新闻栏目并返回修改结果
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public AjaxRes update(NewsColumn nc,String apprpId){
		AjaxRes res=new AjaxRes(); 
		AppendixRelationship apprp = new AppendixRelationship();
		if(nc.getId().equals("0")){
			nc.setCreateTime(new Date());
			nc.setId(UuidUtil.get32UUID());
			nc.setNcurl("test");
			if(!apprpId.equals("")){
				//添加附件关系
				apprp.setId(apprpId);
				List<AppendixRelationship> apprpUis = apprpservice.find(apprp);
				apprpUis.get(0).setRelationNo(nc.getId());
				apprpUis.get(0).setRelationTName("news_column");
				apprpservice.update(apprpUis.get(0));
			}
			//用于测试是否添加成功的变量
			Integer a =ncservice.insert(nc);
			if(a==0){
				res.setResMsg("添加失败");
			}else{
				res.setResMsg("添加成功");
			}
		}else{
			if(!apprpId.equals("")){
				//添加附件关系
				apprp.setId(apprpId);
				System.out.println(apprpId);
				List<AppendixRelationship> apprpUis = apprpservice.find(apprp);
				apprpUis.get(0).setRelationNo(nc.getId());
				apprpUis.get(0).setRelationTName("news_column");
				System.out.println(apprpUis.get(0).toString());
				apprpservice.update(apprpUis.get(0));
			}
			//用于测试是否修改成功的变量
			Integer a =ncservice.update(nc);
			if(a==0){
				res.setResMsg("修改失败");
			}else{
				res.setResMsg("修改成功");
			}
		}
		return res;
	}
	@RequestMapping("/delect")
	@ResponseBody
	public AjaxRes delect(NewsColumn nc){
		AjaxRes res = new AjaxRes();
		//用于测试是否添加成功的变量
		Integer delRes = ncservice.delete(nc);
		if(delRes==0){
			res.setResMsg("删除失败");
		}else{
			res.setResMsg("删除成功");
		}
		return res;
	}

	//批量删除
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public AjaxRes deleteBatch(String uiCheck){
		AjaxRes res=new AjaxRes(); 
		List ncList=new ArrayList();
		String[] getArr = uiCheck.split(",");
		for(int i=0;i<getArr.length;i++){
			if(i!=0){
				NewsColumn nc = new NewsColumn();
				nc.setId(getArr[i]);
				ncList.add(nc);
			}
		}
		Integer a = ncservice.deleteBatch(ncList);
		if(a==0){
			res.setResMsg("批量删除失败");
		}else{
			res.setResMsg("批量删除成功");
		}
		return res;
	}
	@RequestMapping("/newsColumnInfo")
	public String newsColumnInfo(Model model,String id){
		NewsColumn nc = new NewsColumn();
		nc.setId(id);
		if(!id.equals("0")){
			List<NewsColumn> ncList = ncservice.find(nc);
			if(ncList.size()>0){
				nc = ncList.get(0);
			}
		}
		model.addAttribute("nc", nc);
		return "pages/newsColumnInfo";
	}
	@RequestMapping("/findByNcId")
	public String findByNcId(Model model,String id){
		System.out.println(1111);
		return "pages/newsColumnInfo";
	}
}
